﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DNMeter
{
    public partial class Reccorder : Form
    {
		private StreamWriter sW = new StreamWriter("LOG.TXT");

		private void LogInfo(string message, params string[] args) {
			string output = string.Format(message, args);
			sW.WriteLine(output);
			if (this.IsDisposed)
			{
				sW.Close();
			}
		}

        private void startReccording() {
			this.webBrowser.StartHTTPAPP();
			this.webBrowser.StartHTTPSAPP();
			this.webBrowser.LocationUrl = this.comboBoxURL.Text;
			this.webBrowser.LoadUrlIntoBrowser(this.comboBoxURL.Text);
			
        }
        public Reccorder()
        {
            InitializeComponent();
        }

        private void toolStripButtonStartRec_Click(object sender, EventArgs e)
        {
			startReccording();
        }

		private void Reccorder_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.webBrowser.Stop();
			this.webBrowser = null;
			
			
			
		}

		private void webBrowser_ProtocolHandlerOnBeginTransaction(object sender, csExWB.ProtocolHandlerOnBeginTransactionEventArgs e)
		{

			LogInfo("####### BT_START ##########\n");
			LogInfo("URL \t{0}",  e.m_URL);
			LogInfo("ADDL_PARAM \r\n{0}",  e.m_AdditionalHeadersToAdd);
			LogInfo("REQ_HDRS \r\n{0}", e.m_RequestHeaders);
			LogInfo("####### BT_END ##########\n");
			

		}

		private void webBrowser_ProtocolHandlerOnResponse(object sender, csExWB.ProtocolHandlerOnResponseEventArgs e)
		{
			LogInfo("####### OR_START ##########\n");
			LogInfo("URL \t{0}", e.m_URL);
			LogInfo("REDIR_URL \t{0}", e.m_RedirectedUrl);
			LogInfo("REDIR_HDRS \r\n{0}", e.m_RedirectHeaders);
			LogInfo("RESP_HDRS \r\n{0}",  e.m_ResponseHeaders);
			LogInfo("####### OR_END ##########");
			

		}

		private void webBrowser_DocumentCompleteEX(object sender, csExWB.DocumentCompleteExEventArgs e)
		{

		}

		private void webBrowser_DocumentComplete(object sender, csExWB.DocumentCompleteEventArgs e)
		{
			
		}

		private void webBrowser_BeforeNavigate2(object sender, csExWB.BeforeNavigate2EventArgs e)
		{
			
		}

		private void webBrowser_NavigateComplete2(object sender, csExWB.NavigateComplete2EventArgs e)
		{
			//Change the text when necessary....
			this.comboBoxURL.Text = webBrowser.LocationUrl;
		}

		private void Reccorder_Load(object sender, EventArgs e)
		{

		}

		private void toolStripButtonForward_Click(object sender, EventArgs e)
		{
			this.webBrowser.GoForward();
		}

		private void toolStripButtonBack_Click(object sender, EventArgs e)
		{
			this.webBrowser.GoBack();
		}

		private void toolStripButtonEndRec_Click(object sender, EventArgs e)
		{
			this.webBrowser.Stop();
		}
    }
}
